package com.zklh.leetcode.other;

/**
 * 快慢指针实现快乐数算法
 *
 * @Author tongnan
 * @Date 2024/4/9
 * @Version 1.0
 */
public class Solution202 {
    public boolean isHappy(int n) {
        int slow = n;
        int quick = n;
        do {
            slow = calculate(slow);
            if (slow == 1) {
                return true;
            }
            quick = calculate(quick);
            quick = calculate(quick);

        } while (slow != quick);

        return false;
    }

    private int calculate(int n) {
        int temp = n;
        int result = 0;
        while (temp >= 10) {
            result += (temp % 10) * (temp % 10);
            temp = temp / 10;
        }
        result += temp * temp;
        return result;
    }

    public static void main(String[] args) {
        Solution202 solution202 = new Solution202();
        System.out.println(solution202.isHappy(2));
    }
}